<?php
session_start();
require_once 'header.php';
function __autoload($className)
	{
    	require_once('class/' . $className . '.class.php');
	}
session::need_member();	
	if(isset($_POST["submit"]))
	{
		$check=new user('user','pass');
		$oldpw=strval(rtrim(trim($_POST["oldpass"])));
			if(isset($_POST["newpw1"]))
			{
				$newpw1=strval(rtrim(trim($_POST["newpw1"])));
				$newpw2=strval(rtrim(trim($_POST["newpw2"])));
				if(isset($newpw1)&&(strlen($newpw1)<6)&&(strlen($newpw1)>0))
				{
					$_SESSION["erro"]="Minimum New Password lengh is 6 characters ";
					redirect::location("accouninfo.php");
				}
				else if($newpw1 != $newpw2)
				{
					$_SESSION["erro"]="Password 1 ad Password 2 have to be the same";
					redirect::location("accouninfo.php");
				}
				else
				{
					$gethashpw=$check->gethashpw($newpw1);
					$update['password']=$gethashpw;
					
				}
			}
			if(!empty($_POST["email"]))
			{
				$email=rtrim(trim($_POST["email"]));
				$Check=verify::check($email);
				if($Check==0)
				{
					$update['email']=$email;
				}
				
			}
			$checkpw=$check->checkpw($oldpw);
			if($checkpw==1)
			{
				$userid=session::getuserid();
				$condition=array(
				'userid' => $userid,
				);
				
				$up=database::Update('accouninfo.php',$condition,$update);
				if($up==true)
				{
					$_SESSION["erro"]="Successful ! Your account has been updated";
					header('location:membercp.php');
				}
			}
			else
			{
				$_SESSION["erro"]="Invalid Old Password";
				header('location:accouninfo.php');	
			}
			
		}
		else
	{
		
		$userid=session::getuserid();
		$option=array(
		'select' => 'email',
		'condition' => array(
		'userid' => $userid,
		),
		);
		$getinfo=database::LimitQuery('userinfo',$option);
		require_once 'html/accountinfo.php';
	}
		
	


?>